package com.android.common.mvp.view;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.android.common.R;

public class ToolBarBinding {
    private final ImageView mIvBack;
    private final TextView mTvToolbarLeft;
    private final TextView mTvTitle;
    private final LinearLayout mLlToolbarRightGroup;
    private BackClickListener mBackClickListener;
    private ToolbarLeftListener mToolbarLeftListener;

    public ToolBarBinding(ConstraintLayout toolbar){
        mIvBack = toolbar.findViewById(R.id.iv_back);
        mTvToolbarLeft = toolbar.findViewById(R.id.tv_toolbar_left);
        mTvTitle = toolbar.findViewById(R.id.tv_title);
        mLlToolbarRightGroup = toolbar.findViewById(R.id.ll_toolbar_right_group);
        initEvent();
    }

    public void setToolbarRightViews(View view){
        if (view != null){
            mLlToolbarRightGroup.addView(view);
        }
    }

    private void initEvent(){
        mIvBack.setOnClickListener(view -> {
            if (null != mBackClickListener){
                mBackClickListener.onBackClickListener();
            }
        });

        mTvToolbarLeft.setOnClickListener(view -> {
            if (null != mToolbarLeftListener){
                mToolbarLeftListener.onToolbarLeftClickListener();
            }
        });

    }

    public void setTitleText(String text){
        if (!TextUtils.isEmpty(text)){
            mTvTitle.setText(text);
        }
    }

    public void showBack(boolean isShow){
        if (isShow){
            mIvBack.setVisibility(View.VISIBLE);
        }else {
            mIvBack.setVisibility(View.GONE);
        }
    }

    public void showToolbarLeftTextView(boolean isShow){
        if (isShow){
            mTvToolbarLeft.setVisibility(View.VISIBLE);
        }else {
            mTvToolbarLeft.setVisibility(View.GONE);
        }
    }

    public void setBackClickListener(BackClickListener listener) {
        this.mBackClickListener = listener;
    }

    public void setToolbarLeftListener(ToolbarLeftListener listener) {
        this.mToolbarLeftListener = listener;
    }

    public interface BackClickListener{
        void onBackClickListener();
    }

    public interface ToolbarLeftListener{
        void onToolbarLeftClickListener();
    }
}
